package ych;

import java.awt.Color;
import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
/**
 * A robot that moves along the walls and shoots. It does certain movements when shot
 * or at a certain energy level to confuse the enemy.
 * @author Chris Young
 *
 */
public class Boom01 extends Robot {
  //check how much to move
  double moveAmount; 
  
  /**
   * Move around the walls.
   */
  public void run() {
    // Set colors
    setAllColors(Color.black);
    // Initialize moveAmount to the maximum possible for this battlefield.
    moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());

    // turnLeft to face a wall.
    // getHeading() % 90 means the remainder of
    // getHeading() divided by 90.
    turnLeft(getHeading() % 90);
    ahead(moveAmount);
    // Turn the gun to turn right 90 degrees.
    turnGunRight(90);
    turnRight(90);

    while (true) {   
      // Move up the wall
      ahead(moveAmount);
      // Turn to the next wall
      turnRight(90);
    }
  }

  /**
   * Move away.
   * @param e event when robot is seen.
   */
  public void onHitRobot(HitRobotEvent e) {
    // If he's in front of us, set back up a bit.
    if (e.getBearing() > -90 && e.getBearing() < 90) {
      back(100);
    } // else he's in back of us, so set ahead a bit.
    else {
      ahead(100);
    }
  }
   /**
    * When hit by a bullet, just go backwards. Go back and up a wall. Then do a 360 with the gun. 
    * Do some random movements when the energy level is at 50-55 or < 10.
    * 
    * @param e an event when the robot is hit by a bullet
    */
   public void onHitByBullet(HitByBulletEvent e) {
     back(moveAmount);
     turnRight(90);
     ahead(moveAmount);
     turnGunRight(360);
     //if the robot's energy is 50-55, move in a circle.
     if (getEnergy() > 50 && getEnergy() < 55) {
       for (int i = 0; i < 60; i++) {
         turnRight(6);
         ahead((2 * Math.PI * 100) / 60);
       }
     }
     //if the robot's energy is less than 10, move away from the 
     //enemy robot in a horizontal path away from it.
     if (getEnergy() < 10) {
       turnRight(e.getBearing());
       turnRight(90);
       back(100);
       stop();
     }
   }
  /**
   * scan for robots and shoot.
   * @param e event when robot is scanned by radar.
   */
  public void onScannedRobot(ScannedRobotEvent e) {
    fire(3);
  }
}